<?php


namespace app\api\library\user\getUserInfo;


use app\admin\model\MallWithdrawRecord;
use app\api\model\MallTradeRecordLog;
use app\api\model\MallUser;
use think\facade\Db;

class V
{
    public static function run($param)
    {
        //查询有无openid
        $userId   = _checkEmptyParam($param[ 'userId' ], '用户不能为空');
        $userInfo = MallUser::where(['id' => $param[ 'userId' ]])->find();
        
        if (empty($userInfo)) {
            _outPut(200000, '', '用户不存在');
        }
        $res       = [
            'id'              => $userInfo[ 'id' ],
            'avatar_url'      => $userInfo[ 'avatar_url' ],
            'name'            => $userInfo[ 'name' ],
            'balance'         => getPrice($userInfo[ 'balance' ]),
            'failure_reward'  => 0,
            'success_reward'  => 0,
            'withdraw_amount' => 0,
        ];
        $tradeInfo = Db::name('mall_trade_record_log')->where([['user_id', '=', $userId], ['trade_type', 'in', [3, 4, 7]]])->field('SUM(amount) as amount,trade_type')->group('trade_type')->select();
        foreach ($tradeInfo as $value) {
            if ($value[ 'trade_type' ] == 3) {
                $res[ 'failure_reward' ] = getPrice($value[ 'amount' ]);
            } elseif ($value[ 'trade_type' ] == 4) {
                $res[ 'success_reward' ] -= getPrice($value[ 'amount' ]);
            } else {
                $res[ 'success_reward' ] = getPrice($value[ 'amount' ]);
            }
        }
        $withDraw                 = MallWithdrawRecord::where([['user_id', '=', $userId], ['status', '=', 1]])->value('SUM(amount)');
        $res[ 'withdraw_amount' ] = getPrice($withDraw);
        _outPut(200, $res);
    }
}